 * WINDOWS PREAMBLE. 
#delimit ;
global rawDir "LOCATION OF RAW CPS DATA";
global dataDir "WORKING DIRECTORY";
global currDir "PROGRAM DIRECTORY";
cd "$currDir" ;
capture log close ;
clear all ;
log using 2_SingleWomen_simCredit.log, replace text;
***************************************************************************;
* GENERATE SIMULATED CREDIT ;
***************************************************************************;

local beginTime "DateTime: $S_DATE $S_TIME";
set more off ;
set linesize 200 ;
set mem 5g ;
set seed 123456789 ;

cd "$dataDir" ;

***************************************************************************;
* A LITTLE PROGRAM THAT TELLS US THE SIZE OF DATA IN MEMORY IN GIGS AND MEGS ;
***************************************************************************;
program define sizer ;
	qui des, short ;
	di "OBSERVATIONS: " r(N) ;
	di "DATA IN MEMORY (gbs): " round((r(width)+4)*r(N)/1024^3,.001) ;
	di "DATA IN MEMORY (mbs): " round((r(width)+4)*r(N)/1024^3,.00001)*1000 ;
	end ;


***************************************************************************;
* SIMULATED MEASURES ;
***************************************************************************;

program define simulator ;
	args year suffix sample ;
	di "BEGIN SIMULATOR - `year' `suffix' `sample'" ;

	if "`sample'"=="uniform"{;
		* CREATE A UNIFORM DISTRIBUTION OF EARNINGS ;
		use cpi ;
		keep if year >= 1984 & year <= 2013 ;
		expand 100 ;
		sort year ;
		by year : gen pwages = 500 * (_n) ;
		by year : gen earn = 5 * (_n-1) ;
		expand 52 ;
		sort year pwages ;
		by year pwages : gen state = _n-1 ;
		expand 4 ;
		sort year pwages state ;
		by year pwages state : gen byte depx = _n-1 ;
		gen byte famsize = depx+1 ;
		gen byte depchild = depx ;
		gen byte children = depx ;
		gen byte mstat = 1 ;
		replace pwages = pwages * (cpi/232.957) ;
		replace earn = earn * (cpi/232.957) ;
		gen byte unearn = 0 ;
		gen byte wtsupp=1 ;
		};

	else {;
		* SIMULATED INSTRUMENT USING THE CPS ;
		use cpi ;
		summ cpi if year == `year' ;
		local cpiSIV = r(mean) ;
		keep if year >= 1984 & year <= 2013 ;
		save cpiSpreadCPS, replace ;
		clear ;
		use if year == `year' using cpsBeforeTaxsim ;
		keep if `sample' ;
		count ;
		gen byte children = qualChild ;
		replace children = 3 if qualChild > 3 ;
		replace famsize = min(qualChild+1,4);
		gen earn = (pwages+swages)/12 ;
		gen unearn = max(otherprop+dividends+pensions+ui,0)/12 ;
		keep wtsupp children state agex depx depchild mstat pwages swages gssi pensions ui otherprop transfers famsize earn unearn;
		rename state state_orig ;
		gen id = _n ;
		cross using cpiSpreadCPS ;
		sizer ;
		capture rm cpiSpreadCPS.dta ;
		foreach var of varlist pwages swages gssi pensions ui otherprop transfers earn unearn {;
			qui replace `var' = `var' * cpi/`cpiSIV' ;
			};
		qui expand 52 ;
		sort id year ;
		by id year : gen byte state = _n - 1 ;
		*drop id ;
		};

	sizer ;
	capture rm msg.txt;
	capture rm taxsim_out.dta;
	ankurs_taxsim, full replace;
	capture rm msg.txt;
	capture rm taxsim_out.dta;
	keep year state children v25 v39 v19 v22 v23 siitax v27 v40 cpi wtsupp famsize earn unearn;
	rename v25 fedEICSIV;
	rename v39 stEICSIV ;  
	rename v19 fedRegTaxSIV ;
	egen ctcSIV = rowtotal(v22 v23) ;
	rename v27 amtSIV ;
	gen stCredSIV = v40-stEICSIV ;
	gen stRegTaxSIV = siitax+v40 ;

	sort state year famsize ;
	merge m:1 state year famsize using afdcparameterstemp2012 ;
	drop if _merge == 2;
	tab famsize _merge ;
	tab year _merge ;
	drop _merge ;
	gen afdcSIV = afdccb*max(afdcmax-max(afdctax*(earn-afdcdis),0)-unearn,0) ;
	replace afdcSIV = 0 if children == 0 ;
	replace afdcSIV = afdcSIV*12 ;
	keep year state children fedEICSIV fedRegTaxSIV ctcSIV amtSIV
		stEICSIV stRegTaxSIV stCredSIV cpi wtsupp afdcSIV ;
	sort year state children ;
	collapse (mean) fedEICSIV fedRegTaxSIV ctcSIV amtSIV stEICSIV stRegTaxSIV stCredSIV afdcSIV cpi [aw=wtsupp], 
		by(year state children) ;
	foreach var of varlist fedEICSIV fedRegTaxSIV
			ctcSIV amtSIV stEICSIV stRegTaxSIV stCredSIV afdcSIV {;
		qui replace `var' = `var' * (232.957/cpi);
		rename `var' `var'_`suffix' ;
		};
	drop cpi ;
	sort year state children ;
	save simulatedmeasures`suffix', replace ;
	clear ;
	end ;


use afdcparameters ;
expand 2 if year == 2011 ;
sort state famsize year  ;
by state famsize year: replace year = 2012 if year == 2011 & _n==2;
expand 2 if year == 2011 ;
sort state famsize year  ;
by state famsize year: replace year = 2013 if year == 2011 & _n==2;
duplicates report state famsize year ;
sort state year famsize ;
save afdcparameterstemp2012, replace ;
clear ;

simulator "" "uni" "uniform" ;
simulator "1982" "82cps" "statefip!=2&statefip!=15&incdisab==0&incss==0&incssi==0&age>=24&age<=48" ;
simulator "1992" "92cps" "statefip!=2&statefip!=15&incdisab==0&incss==0&incssi==0&age>=24&age<=48" ;


use simulatedmeasuresuni ;
foreach suffix in 82cps 92cps {;
	sort year state children ;
	merge 1:1 year state children using simulatedmeasures`suffix' ;
	drop _merge ;
	rm simulatedmeasures`suffix'.dta ;
	};
rm simulatedmeasuresuni.dta ;
rm afdcparameterstemp2012.dta ;
save simulatedmeasures, replace ;
clear ;
disp "`beginTime'" ;
disp "DateTime: $S_DATE $S_TIME";

*/
*;
disp "`beginTime'" ;
disp "DateTime: $S_DATE $S_TIME";
log close ;























